//
//  JobTableViewCell.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/8/10.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "JobTableViewCell.h"
#import "NetworkTool.h"
#import "BRDatePickerView.h"
#import <Masonry.h>

@interface JobTableViewCell()
@property (strong ,nonatomic) UIView *seperateLine;
@end

@implementation JobTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
	self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	
	if (self)
	{
		self.selectionStyle = UITableViewCellSelectionStyleNone;
		[self createUI];
	}
	
	return self;
}

- (void)createUI
{
	//分割线
	_seperateLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
	_seperateLine.backgroundColor = UISeparatedColorRGB;
	
	_jobName = [[UILabel alloc] initWithFrame:CGRectMake(10, _seperateLine.height+10, SW(2), 25)];
	_experience = [[UILabel alloc] initWithFrame:CGRectMake(12, _jobName.y+_jobName.height+8, SW(5), 18)];
	_education = [[UILabel alloc] initWithFrame:CGRectMake(_experience.x+_experience.width+10, _experience.y, SW(3), 18)];
	_companyLogo = [[UIImageView alloc] initWithFrame:CGRectMake(10, _experience.y+_experience.height+25, SW(8), SW(8))];
	_companyName = [[UILabel alloc] initWithFrame:CGRectMake(10+_companyLogo.width+10, _companyLogo.y, SW(1), 18)];
	_companyAddress = [[UILabel alloc] initWithFrame:CGRectMake(_companyName.x, _companyLogo.y+25, SW(2), 18)];
	_salary = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-SW(3)-10, _jobName.y, SW(3), 18)];
	_postDate = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-SW(3)-10, _experience.y, SW(3), 16)];
	_postDate.font = [UIFont systemFontOfSize:13];
	_distance = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-SW(2)-10, _companyName.y, SW(2), 16)];
	
	[self.contentView addSubview:_seperateLine];
	[self.contentView addSubview:_jobName];
	[self.contentView addSubview:_experience];
	[self.contentView addSubview:_education];
	[self.contentView addSubview:_companyLogo];
	[self.contentView addSubview:_companyName];
	[self.contentView addSubview:_companyAddress];
	[self.contentView addSubview:_salary];
	[self.contentView addSubview:_postDate];
	[self.contentView addSubview:_distance];
	
}

- (void)setJobInfo:(JobInfoForRecruitIndex *)jobInfo
{
	if (_jobInfo != jobInfo) {
		_jobInfo = jobInfo;
		
		_jobName.text = jobInfo.category_title;
		_jobName.font = [UIFont boldSystemFontOfSize:17];
		
		//用富文本在标签中实现工作经验和学历的图片
		if (jobInfo.work_life.length > 0) {
			NSMutableAttributedString *expAttri = [[NSMutableAttributedString alloc] initWithString:jobInfo.work_life];
			NSTextAttachment *expAttch = [[NSTextAttachment alloc] init];
			expAttch.image = [UIImage imageNamed:@"job_icon_exp"];
			expAttch.bounds = CGRectMake(-2, -3, 15, 15);
			NSAttributedString *expString = [NSAttributedString attributedStringWithAttachment:expAttch];
			[expAttri insertAttributedString:expString atIndex:0];
			[expAttri addAttribute:NSForegroundColorAttributeName value:RGB(132, 132, 132) range:NSMakeRange(1, jobInfo.work_life.length)];
			_experience.attributedText = expAttri;
			_experience.font = [UIFont systemFontOfSize:13];
		}
		
		NSMutableAttributedString *eduAttri = [[NSMutableAttributedString alloc] initWithString:jobInfo.education];
		NSTextAttachment *eduAttch = [[NSTextAttachment alloc] init];
		eduAttch.image = [UIImage imageNamed:@"job_icon_edu"];
		eduAttch.bounds = CGRectMake(-2, -2, 15, 15);
		NSAttributedString *eduString = [NSAttributedString attributedStringWithAttachment:eduAttch];
		[eduAttri insertAttributedString:eduString atIndex:0];
		[eduAttri addAttribute:NSForegroundColorAttributeName value:RGB(132, 132, 132) range:NSMakeRange(1, jobInfo.education.length)];
		_education.attributedText = eduAttri;
		_education.font = [UIFont systemFontOfSize:13];
		
		[_companyLogo sd_setImageWithURL:[NSURL URLWithString:jobInfo.logo] placeholderImage:IMAGE(@"placeHolder")];
		
		if ([jobInfo isKindOfClass:[JobInfoForRecruitIndex class]]) {
			//_companyName.text = jobInfo.company_name;
			_companyName.text = jobInfo.title;
		}
		else
			_companyName.text = jobInfo.title;
		
		_companyName.font = [UIFont systemFontOfSize:14];
		if (![NSString isEmpty:jobInfo.address]) {
			NSMutableAttributedString *comAddrAttri = [[NSMutableAttributedString alloc] initWithString:jobInfo.address];
			NSTextAttachment *comAddrAttch = [[NSTextAttachment alloc] init];
			comAddrAttch.image = [UIImage imageNamed:@"job_icon_address"];
			comAddrAttch.bounds = CGRectMake(-2, -2, 15, 15);
			NSAttributedString *comAddrString = [NSAttributedString attributedStringWithAttachment:comAddrAttch];
			[comAddrAttri insertAttributedString:comAddrString atIndex:0];
			[comAddrAttri addAttribute:NSForegroundColorAttributeName value:RGB(148, 148, 148) range:NSMakeRange(1, jobInfo.address.length)];
			_companyAddress.attributedText = comAddrAttri;
			_companyAddress.font = [UIFont systemFontOfSize:14];
		}
		
		_salary.text = jobInfo.salary;
		_salary.textAlignment = NSTextAlignmentRight;
		_salary.textColor = [UIColor redColor];
		
		NSDate *postDate = [NSDate dateWithTimeIntervalSince1970:jobInfo.create_time.doubleValue];
		NSString *postDateStr = [NSDate br_getDateString:postDate format:@"yyyy年MM月dd日"];
		_postDate.text = postDateStr;
		_postDate.textAlignment = NSTextAlignmentRight;
		_postDate.textColor = RGB(196, 196, 196);
		
		/*
		NSMutableAttributedString *disAttri = [[NSMutableAttributedString alloc] initWithString:@"距离你1500米"];
		NSTextAttachment *disAttch = [[NSTextAttachment alloc] init];
		disAttch.image = [UIImage imageNamed:@"job_icon_distance"];
		disAttch.bounds = CGRectMake(-2, -2, 18, 18);
		NSAttributedString *disString = [NSAttributedString attributedStringWithAttachment:disAttch];
		[disAttri insertAttributedString:disString atIndex:0];
		[disAttri addAttribute:NSForegroundColorAttributeName value:RGB(148, 148, 148) range:NSMakeRange(1, 8)];
		_distance.attributedText = disAttri;
		_distance.textAlignment = NSTextAlignmentRight;
         */
		
	}
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
